//
//  MyAddressEditViewController.m
//  reSearchDemo
//
//  Created by helfy  on 15-4-16.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "MyAddressEditViewController.h"
#import "CameraActionSheet.h"
#import "KLocatePickView.h"
#import "KLocation.h"
@interface MyAddressEditViewController ()
{
    NSDictionary *addressDic;
    
    NSString *province;
    NSString *city;
    NSString *region;
    
    
    
}
@end

@implementation MyAddressEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isSectionMode = NO;
  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveBtnPressed)];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setupData
{
    [super setupData];
    
    YMParameterCellObj *obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeTextField];
    obj.name=obj.title = @"收货人";
    obj.key = @"consignee";
    obj.isRequired =YES;
    if(addressDic)obj.value = addressDic[obj.key];
    obj.cellHeigth =45;
    UITextField *textFiled = (UITextField *)obj.accessoryView;
    textFiled.placeholder = @"名字";
    obj.accessoryViewWidth = [[UIScreen mainScreen] bounds].size.width-130;
    [self.cellObjs addObject:obj];
    
    
    obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeTextField];
     obj.name=obj.title = @"手机号码";
    obj.cellHeigth =45;
    obj.key = @"phone";
      obj.isRequired =YES;
    if(addressDic)obj.value = addressDic[obj.key];
    obj.regex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    textFiled = (UITextField *)obj.accessoryView;
    textFiled.placeholder = @"11位手机号";
    obj.accessoryViewWidth = [[UIScreen mainScreen] bounds].size.width-130;
    [self.cellObjs addObject:obj];
    
    
    obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeLabel];
    obj.title = @"选择地区";
    obj.cellHeigth =45;
    obj.cellAction = @selector(showLoctionPicker);
    UILabel*lable = (UILabel *)obj.accessoryView;
    lable.text = @"地区信息";
    lable.textAlignment= NSTextAlignmentLeft;
    lable.textColor =[UIColor grayColor];
    if(addressDic)obj.value = [NSString stringWithFormat:@"%@ %@ %@",addressDic[@"province"],addressDic[@"town"],addressDic[@"region"]];
    
    obj.accessoryViewWidth = [[UIScreen mainScreen] bounds].size.width-130;

    [self.cellObjs addObject:obj];
    
    obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeTextField];
     obj.name=obj.title = @"详细地址";
    obj.cellHeigth =60;
    obj.isRequired =YES;
    obj.key = @"address";
    if(addressDic)obj.value = addressDic[obj.key];
    textFiled = (UITextField *)obj.accessoryView;
    textFiled.placeholder = @"街道门牌信息";
    obj.accessoryViewWidth = [[UIScreen mainScreen] bounds].size.width-130;

    [self.cellObjs addObject:obj];
    
    
    obj = [[YMParameterCellObj alloc] initWithObjType:YMParameterCellObjTypeTextField];
    obj.name= obj.title = @"邮政编码";
    obj.cellHeigth =45;
    obj.isRequired =YES;
    obj.key = @"postcode";
    obj.regex = @"\\d{6}";
    
    if(addressDic)obj.value = addressDic[obj.key];
    textFiled = (UITextField *)obj.accessoryView;
    textFiled.placeholder = @"邮政编码";
    textFiled.keyboardType = UIKeyboardTypeNamePhonePad;
    obj.accessoryViewWidth = [[UIScreen mainScreen] bounds].size.width-130;

    [self.cellObjs addObject:obj];
    
}
-(void)setAddress:(NSDictionary *)address
{
    addressDic= address;
    province=   addressDic[@"province"];
    city=addressDic[@"town"];
    region =addressDic[@"region"];
    if(address == nil)
    {
        self.title = @"新增地址";
    }
    else
    {
        self.title = @"修改地址";
    }
}


-(void)showLoctionPicker
{
    
    [self.view endEditing:YES];
    KLocatePickView *locateView = [[KLocatePickView alloc] initWithTitle:@"选择城市" delegate:self];
    locateView.tag = 200;
    [locateView showInView:self.view];
}

#pragma mark - Request  新增/修改
-(void)saveBtnPressed
{
//    CameraActionSheet *actionSheet = [[CameraActionSheet alloc] initWithActionTitle:nil TextViews:nil CancelTitle:@"取消" withDelegate:self otherButtonTitles:@"从相册选择", @"拍一张", nil];
//    [actionSheet show];
//
    if(addressDic)
    {
    
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id)self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"保存",@"设为默认地址", nil];
    sheet.tag = 100;
    [sheet showInView:self.view];
    }
    else{
        [self save];
    }

}

-(void)save
{
 
    [self.view endEditing:YES];
    for (YMParameterCellObj *obj in self.cellObjs) {
        NSString *errorStr = [obj check];
        if (errorStr.length >0) {
            [self showText:errorStr];
            return;
        }
    }
    
    if ([super startRequest]) {
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        for (YMParameterCellObj *obj in self.cellObjs) {
            if(obj.key && obj.value)
            {
                [dic setObject:obj.value forKey:obj.key];
            }
        }
        [dic setObject:[[BSEngine currentEngine] user].uid forKey:@"uid"];
        if(addressDic)
        {
            [dic setObject:[addressDic objectForKey:@"addressid"] forKey:@"addressid"];
        }
        
        [dic setValue:province forKey:@"province"];
        [dic setValue:city forKey:@"town"];
        [dic setValue:region forKey:@"region"];
        
        [client requestFor:dic methodName:@"User/Api/setMyaddress"];
    }
}
-(void)setDefauleAddress
{
    if ([super startRequest]) {
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        [dic setObject:[[BSEngine currentEngine] user].uid forKey:@"uid"];
        if(addressDic)
        {
            [dic setObject:[addressDic objectForKey:@"addressid"] forKey:@"addressid"];
            [dic setObject:[NSString stringWithFormat:@"%i",![[addressDic objectForKey:@"isdefault"] boolValue]] forKey:@"isdefault"];
        }
        
        
        
        [client requestFor:dic methodName:@"User/Api/setMyaddress_isdefault"];
    }
}

- (BOOL)requestDidFinish:(id)sender obj:(NSDictionary *)obj {
    if ([super requestDidFinish:sender obj:obj]) {
        NSDictionary *data = obj[@"data"];
        if([data isKindOfClass:[NSDictionary class]])
        {
            NSString *debugMsg = data[@"msg"];
            [self showText:debugMsg];
        }
        else{
            [self.navigationController popViewControllerAnimated:YES];
            
        }
    
    }
    return NO;
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if(actionSheet.cancelButtonIndex != buttonIndex)
    {
        if(actionSheet.tag == 100)
        {
           
            switch (buttonIndex ) {
                case 0:
                {
                    [self save];
                }
                    break;
                case 1:
                {
                    [self setDefauleAddress];
                }
                    
                    break;
                default:
                    break;
            }
          
            
        
        }
    
        
        
    }
    
    if(actionSheet.tag == 200)
    {
    
    if(buttonIndex == 1) {
        KLocatePickView *locateView = (KLocatePickView *)actionSheet;
        KLocation *location = locateView.locate;
        province = location.state;
        city = location.city;
        region = @" ";
        YMParameterCellObj *obj = self.cellObjs[2];
        obj.value = [NSString stringWithFormat:@"%@ %@",province,city];
        [self.tableView reloadData];
    }
    }
}
@end
